<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
	class Post_edit extends Admin_Controller{
		private $list_cat = '';
		public function __construct(){
			parent::__construct();
			$this->load->model('database_model');
		}

		public function index(){
			if(!is_user_logged_in()){
				redirect('admin/login', 'refresh');
			}

			$post_id = $this->uri->segment(3);
			$post_id = $this->security->xss_clean($post_id);
			$post_id = trim($post_id);
			if((int)$post_id > 0){
				$posts = $this->database_model->GetWhere('ci_posts', array('ID' => $post_id, 'post_type' => 'post'));
				if($posts->num_rows() > 0){
					$post = $posts->row();
					$terms_post = $this->database_model->GetWhere('ci_relations', array('post_id' => $post->ID))->result();
					$i = 0;
					$categories = new stdClass;
					foreach($terms_post as $term){
						$categories->$i->term_id = $term->term_id;
						$i++;
					}
					$terms = $this->database_model->Get('ci_terms')->result_array();
					$terms_tree = $this->categoryTree($terms);
					$data = array(
						'header' => 'Edit post',
						'menu_active' =>"3.1",
						'ID' => $post_id,
						'post_title' => $post->post_title,
						'post_content' => $post->post_content,
						'post_date' => $post->post_date,
						'post_status' => $post->post_status,
						'post_featured' => $post->post_featured,
						'post_terms' => $categories,
						'terms_tree' => $terms_tree,
						);
					$this->load->view('admin/post_edit', $data);
				}else{
					redirect('admin/post_new','refresh');
				}
			}else{
				redirect('admin/post_new','refresh');
			}
		}

		public function postUpdate(){
			if(!serverReferer()){
				redirect('admin/post_new', 'refresh');
			}

			$this->load->library('form_validation');
			$this->form_validation->set_rules('post_title', 'Post title', 'trim|required|min_length[1]|max_length[255]|xss_clean');
			if($this->form_validation->run() === FALSE){
				$json = array(
					'status' => '0',
					'message' => validation_errors()
					);
				echo json_encode($json);
			}else{
				$post_id       = $this->input->post('post_id', TRUE);
				$post_title    = $this->input->post('post_title', TRUE);
				$post_content  = $this->input->post('area');
				$post_date     = $this->input->post('post_date', TRUE);
				$post_author   = $this->input->post('post_author', TRUE);
				$post_status   = $this->input->post('post_status', TRUE);
				$post_featured = $this->input->post('post_featured', TRUE);
				$post_type     = 'post';
				
				$post_cate     = $this->input->post('terms', TRUE);

				if(empty($post_date)) $post_date = getCurrentDate();
				$data = array(
					'post_title'    => $post_title,
					'post_content'  => $post_content,
					'post_status'   => $post_status,
					'post_featured' => $post_featured,
					'post_date_mdf' => $post_date
					);
				try{
					$post_id = (int) $post_id;
					$this->database_model->Update('ci_posts', $data, array('ID' => $post_id));
					// Insert ci_relations
					$this->database_model->Delete('ci_relations', array('post_id' => $post_id));
					if(is_array($post_cate) && sizeof($post_cate)){
						foreach($post_cate as $term_id){
							$data = array(
								'post_id' => $post_id,
								'term_id' => $term_id
								);
							$this->database_model->Insert('ci_relations', $data);
						}
					}else{
						$data = array(
							'post_id' => $post_id,
							'term_id' => '1'
							);
						$this->database_model->Insert('ci_relations', $data);
					}
					$json = array(
						'status' => 1,
						'message' => site_url().'/admin/post_edit/'.$post_id
					);
					echo json_encode($json);
				}catch(Exceptions $e ){
					$json = array(
						'status' => 0,
						'message' => 'Error database. Try again'
					);
					echo json_encode($json);
				}
			}
		}

		public function categoryTree($terms, $parent = 0){
			if(!is_user_logged_in()){
				redirect('admin/login', 'refresh');
			}

			$menu_tmp = array();
			foreach($terms as $key => $term){
				if((int) $term['term_parent'] == (int) $parent){
					$menu_tmp[] = $term;
					unset($terms[$key]);
				}
			}
			if($menu_tmp){
				$this->list_cat .= '<ul>';
				foreach($menu_tmp as $item){
					$this->list_cat .= '<li>';
					$this->list_cat .= '<label class="pointer" for="cate-'.$item['term_slug'].'"><input id="cate-'.$item['term_slug'].'" type="checkbox" name="terms[]" value="'.$item['term_id'].'">'.$item['term_name'].'</label>';
					$this->categoryTree($terms, $item['term_id']);
					$this->list_cat .= '</li>';
				}
				$this->list_cat .= '</ul>';
			}
			return $this->list_cat;
		}
	}
?>